  

<?php

class SmartlockDao {

    var $connection;

    function SmartlockDao($connection) {
        $this->connection = $connection;
    }

    function create($operateur, $reference, $lat, $lng) {
        $r = $this->connection->query("INSERT INTO t_velo_vel(VEL_operateur, VEL_type, VEL_date_mes, VEL_caution, VEL_caution_tva, VEL_distance,VEL_reference,VEL_actif) VALUES( \"$operateur\", \"1\", \"0\",\"0\",\"1\",\"0\", \"$reference\" , \"1\")");
        $id = $this->connection->querySingle("select VEL_id from t_velo_vel where VEL_operateur=\"$operateur\" and VEL_reference=\"$reference\" ");
        $vel_id = $id['VEL_id'];
        $this->connection->query("INSERT INTO  ts_etat_velo_etv (`ETV_velo`, `ETV_latitude`, `ETV_longitude`, `ETV_abonnement`) VALUES ( \"$vel_id\", \"$lat\", \"$lng\",NULL)");
    }

    public function findSmartlocks($operateur, $smartlockId) {
        $sql = "SELECT ETV_velo, ETV_latitude, ETV_longitude, ETV_abonnement,"
                . " VEL_operateur, VEL_actif, VEL_reference FROM ts_etat_velo_etv JOIN t_velo_vel "
                . "ON (ETV_velo = $smartlockId and VEL_operateur=$operateur)";
        $r = $this->connection->querySingle($sql);
        return $r;
    }

    public function findallSmartlocks($operateur) {
        $sql = "SELECT ETV_velo, ETV_latitude, ETV_longitude, ETV_abonnement,"
                . " VEL_operateur, VEL_actif, VEL_reference FROM ts_etat_velo_etv JOIN t_velo_vel "
                . "ON (ETV_velo = VEL_id and VEL_operateur=$operateur)";
        $r = $this->connection->queryAll($sql);
        return $r;
    }

    public function findallAvailableSmartlocks($operateur) {
        $sql = "SELECT ETV_velo, ETV_latitude, ETV_longitude, ETV_abonnement, VEL_operateur, VEL_actif, VEL_reference"
                . " FROM ts_etat_velo_etv "
                . " JOIN t_velo_vel "
                . " ON (ETV_velo = VEL_id and VEL_operateur=$operateur)  where  VEL_actif=1 ";
        return $this->connection->queryAll($sql);
    }

    public function update($operateur, $id, $lat, $lng, $reference, $abonnement, $actif) {
        if (empty($abonnement)) {
            $sql = "UPDATE ts_etat_velo_etv SET ETV_abonnement=NULL, ETV_latitude=\"$lat\", ETV_longitude=\"$lng\" WHERE ETV_velo=$id";
        } else {
            $sql = "UPDATE ts_etat_velo_etv SET ETV_abonnement=\"$abonnement\", ETV_latitude=\"$lat\", ETV_longitude=\"$lng\" WHERE ETV_velo=$id";
        }

        $this->connection->query($sql);

        $sql = "UPDATE t_velo_vel SET VEL_reference=\"$reference\", VEL_actif=\"$actif\" where VEL_id=$id";
        $this->connection->query($sql);
    }

}
